package cn.ffcs.bpm.web;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.time.DateUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.PageControl;
import cn.ffcs.bpm.model.DocumentType;
import cn.ffcs.bpm.model.NextTep;
import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.ProcessConfig;
import cn.ffcs.bpm.model.ProcessConfigDetail;
import cn.ffcs.bpm.model.ProcessStep;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpm.model.Transactor;
import cn.ffcs.bpm.model.TransactorType;
import cn.ffcs.bpm.service.DocumentTypeService;
import cn.ffcs.bpm.service.NextTepService;
import cn.ffcs.bpm.service.ProcTranRecordService;
import cn.ffcs.bpm.service.ProcessConfigDetailService;
import cn.ffcs.bpm.service.ProcessConfigService;
import cn.ffcs.bpm.service.ProcessStepService;
import cn.ffcs.bpm.service.TransactProcessStatusService;
import cn.ffcs.bpm.service.TransactorService;
import cn.ffcs.bpm.service.TransactorTypeService;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.model.NewprodReq;
import cn.ffcs.businessrequest.model.ProdAdvReq;
import cn.ffcs.businessrequest.model.ProdReq;
import cn.ffcs.businessrequest.model.ReqDelay;
import cn.ffcs.businessrequest.model.ReqDocu;
import cn.ffcs.businessrequest.model.ReqProgress;
import cn.ffcs.businessrequest.service.BusinessRequestService;
import cn.ffcs.businessrequest.service.INewprodReqService;
import cn.ffcs.businessrequest.service.IProdAdvReqService;
import cn.ffcs.businessrequest.service.ReqDocuService;
import cn.ffcs.businessrequest.service.ReqProgressService;
import cn.ffcs.organ.model.OrgRelaPerson;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;
import cn.ffcs.organ.service.OrgRelaPersonService;
import cn.ffcs.security.model.User;
import cn.ffcs.security.model.UserResource;
import cn.ffcs.util.GetSet;
import cn.ffcs.util.MyList;
import cn.ffcs.util.context.ApplicationContextUtil;
import cn.ffcs.util.converter.DateConverter;
import cn.ffcs.util.ext.FieldObject;

import com.opensymphony.xwork2.ActionContext;

/**
 * 流程办理状态action层，实现页面业务逻辑<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class TransactProcessStatusAction<P extends TransactProcessStatus>
        extends BizProcessManageAction<P>
{
    private static final int BUFFER_SIZE = 512 * 1024;
    /** 流程办理POJO **/
    private P transactProcessStatus;
    /** 办理基本信息POJO **/
    private BusinessRequest businessRequest;
    /** 办理人信息POJO **/
    private Transactor transactor;
    /** 流程历史信息POJO **/
    private ProcTranRecord procTranRecord;
    /** 办理状态服务 **/
    private TransactProcessStatusService transactProcessStatusService;
    /** 业务需求基本信息服务 **/
    private BusinessRequestService businessRequestService;
    
    /** 需求进展情况服务 **/
    private ReqProgressService<ReqProgress> reqProgressService;
    /** 需求延迟信息服务 **/
    private cn.ffcs.businessrequest.service.ReqDelayService<cn.ffcs.businessrequest.model.ReqDelay> reqDelayService;
    
    
    /** 附件列表数据 **/
    private List<ReqDocu> reqDocus;
    /** 文档服务 **/
    private ReqDocuService reqDocuService;
    /** 文档上传 **/
    private List<File> uploads;
    /** 上传文档名称 **/
    private List<String> fileNames;
    /** 上传文档名称 **/
    private List<String> uploadContentTypes;

    /** 流程节点服务 **/
    private ProcessConfigDetailService processConfigDetailService;
    /** 流程步骤服务 **/
    private ProcessStepService processStepService;
    /** 流程配置服务 **/
    private ProcessConfigService processConfigService;
    /** 办理人服务 **/
    private TransactorService transactorService;
    /** 办理人类型服务 **/
    private TransactorTypeService transactorTypeService;
    /** 办理人类型服务 **/
    private NextTepService nextTepService;
    /** 办理人类型服务 **/
    private ProcTranRecordService procTranRecordService;
    /** 流程配置数据集 **/
    private List<ProcessConfig> processConfigs;
    /** 流程步骤数据集 **/
    private List<ProcessStep> processSteps;
    /** 办理人类型数据集 **/
    private List<Transactor> transactors;
    /** 办理人类型数据集 **/
    private List<TransactorType> transactorTypes;
    /** 下一节点数据集 **/
    private List<NextTep> nextTeps;
    /** 流程步骤数据集 **/
    private List<ProcessConfigDetail> processConfigDetails;
    /** 流程步骤数据集 **/
    private List<P> transactProcessStatusList;
    /** 流程步骤数据集 **/
    private List<ProcTranRecord> procTranRecords;
    /** 业务流程单数据集 **/
    private List<BusinessRequest> businessRequests;
    /** 业务流程阅办单数据集 **/
    private List<BusinessRequest> yueBusinessRequests;
    /** 产品需求实例 **/
    private List<DocumentType> documentTypes;
    /** 组织服务 **/
    private OrgRelaPersonService orgRelaPersonService;
    /** 新产品需求服务 **/
    private INewprodReqService newprodReqService;
    /** 产品改进需求服务 **/
    private IProdAdvReqService prodAdvReqService;
    /** 文档类型服务 **/
    private DocumentTypeService documentTypeService;
    /** 是否首页,更多 **/
    private String isIndex;
    /** 需求单类型 **/
    private String businessType;
    /** 新增产品需求 **/
    private NewprodReq newprodReq;
    /** 改进产品需求 **/
    private ProdAdvReq prodAdvReq;
    /** 产品需求实例 **/
    private ProdReq prodReq;
    /** 已上传的未关联需求单的附件id **/
    private String documentIds;
    
    /** 改进产品需求单列表 **/
    private List<ProdAdvReq> prodAdvReqs;

    /** 需求延迟 **/
    private ReqDelay reqDelay;
    /** 需求延迟数据 **/
    private List<ReqDelay> reqDelayss;
    /** 需求进展数据 **/
    private ReqProgress reqProgress;
    /** 需求进展数据 **/
    private List<ReqProgress> reqProgressess;
    /** 文档上传 **/
    private List<File> uploadspro;
    /**  保存需求进展返回消息 **/
    private String returnMsg;
    /**  启动需求单发起人 **/
    private String startPerson;
    /**  启动需求单发起部门 **/
    private String startDept;
    /**  办理意见 **/
    private String advice;
    
    private int num=0;

    /**
     * 初始化业务数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String init()
    {
        if (businessRequest != null)
        {
            Criterion cond1 = Restrictions.eq("refEntity", businessRequest
                    .getClass().getName());
            Criterion cond2 = Restrictions.eq("refEntityId", businessRequest
                    .getId().toString());
            Criterion condDocu = Restrictions.and(cond1, cond2);
            reqDocus = reqDocuService.findBy(condDocu);
            Criterion cond3 = Restrictions.eq("refEntityId", businessRequest);
            Criterion condProcTranRecord = Restrictions.and(cond1, cond3);
            procTranRecords = procTranRecordService.findByAndOrder(
                    condProcTranRecord, "id", "asc");
            ///需求单关联的改进需求单列表
            Criterion cond5 = Restrictions.eq("relaReq", businessRequest);
            prodAdvReqs=prodAdvReqService.findBy(cond5);
            
        }
        return SUCCESS;
    }

    /**
     * 查询显示当前操作人员待办单
     * @return
     */
    public String findCurBusinessRequest()
    {
        HashMap<String, Object> conds = super.getConds();
        if (conds == null)
        {
            conds = new HashMap<String, Object>();
        }
        User user=ApplicationContextUtil.getUserContextObject().getCurrUser();
        conds.put("transactor.userId", user.getPersonID());
        conds.put("transactor.tranType", "1");
        businessRequests = transactorService.findByPage(conds);
        //设置到期时间
        for (Iterator iterator = businessRequests.iterator(); iterator.hasNext();)
        {
            BusinessRequest businessRequest = (BusinessRequest) iterator.next();
            Criterion cond1 = Restrictions.eq("refEntity", businessRequest
                    .getClass().getName());
            Criterion cond2 = Restrictions.eq("refEntityId", businessRequest);
            Criterion condDocu = Restrictions.and(cond1, cond2);
            List<TransactProcessStatus> list = transactProcessStatusService.findbytp(condDocu);
            TransactProcessStatus transactProcessStatus = list==null?null:list.get(0);
            businessRequest.setTransactProcessStatus(transactProcessStatus);
            if (transactProcessStatus!= null && transactProcessStatus.getTimeLimit() != null){
                businessRequest.setTimeLimit(transactProcessStatus.getTimeLimit());
                Date timeLimit = transactProcessStatus.getTimeLimit();
                Date sysdate = new Date();
                long totalDate = timeLimit.getTime() - sysdate.getTime();
                totalDate = totalDate/(1000*60*60*24);
                businessRequest.setLimitDate(String.valueOf(totalDate));
            }
            
            //进展信息
            Criterion cond4 = Restrictions.eq("requestBusiness", businessRequest);
            //当前步骤进展信息
            Criterion cond6 = Restrictions.eq("processConfigDetail", transactProcessStatus.getCurrProcessConfigDetail());
            Criterion pcdcont = Restrictions.and(cond4, cond6);
            List<ReqProgress> reqProgressess = reqProgressService.findBy(pcdcont);
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            int week = cal.get(Calendar.DAY_OF_WEEK);
            if(week == 6 && (reqProgressess == null || reqProgressess.size() <= 0)){
                businessRequest.setIsReqProgresse("0");
            }else{
                businessRequest.setIsReqProgresse("1");
            }
        }
        
        /////显示改进单待办:状态为拒绝或新增，发起人为自己的需求单
        Criterion cond1 = Restrictions.eq("startPerson", user.getPerson());
        Criterion cond2 = Restrictions.eq("tranStatus", "NEW");
        Criterion cond3 = Restrictions.eq("tranStatus", "REFUSE");
        Criterion cond4 = Restrictions.or(cond2, cond3);

        
        
        ///判断有权限的，显示所有提交单；否则，显示本人相关单
        List<UserResource> modelMenus = ApplicationContextUtil.getUserContextObject().getUserResources();
        boolean searchAll = false;
        for (UserResource ur : modelMenus)
        {
            if (ur.getKey().equals("OBJ_ADV_REQ_MAN"))
            {
                searchAll = true;
                break;
            }
        }
        if(searchAll){
            Criterion tmp1 = Restrictions.or(Restrictions.eq("tranStatus", "WAIT"),Restrictions.eq("tranStatus", "PENDING"));
            Criterion tmp2 = Restrictions.or(tmp1,Restrictions.eq("tranStatus", "PASS"));
            cond4=Restrictions.or(cond4,tmp2);
        }
        Criterion cond5 = Restrictions.and(cond1, cond4);
        prodAdvReqs=prodAdvReqService.findBy(cond5);
        
        conds.put("transactor.tranType", "3");
        yueBusinessRequests = transactorService.findByPage(conds);
        /** 判断是否首页更多 **/
        if(isIndex != null && isIndex.equalsIgnoreCase("1"))
        {
            return "listBusinessRequest";
        }
        return SUCCESS;
    }

    /**
     * 查询显示当前操作人员已办单
     * @return
     */
    public String eventBusinessRequest()
    {
        HashMap<String, Object> conds = super.getConds();
        if (conds == null)
        {
            conds = new HashMap<String, Object>();
        }
        conds.put("procTranRecord.transactor", ApplicationContextUtil
                .getUserContextObject().getCurrUser().getPersonID());
        businessRequests = businessRequestService.findOrganized(conds);
        if(pageControl==null){
            pageControl=new PageControl(businessRequests.size(),1,10,0);
        }
        else{
            pageControl=new PageControl(businessRequests.size(),pageControl.getPageNo(),10,0);
        }
        businessRequests=this.offset(businessRequests, pageControl.getPageNo(), pageControl.getPageSize());
        /** 判断是否首页更多 **/
        if(isIndex != null && isIndex.equalsIgnoreCase("1"))
        {
            return "listBusinessRequest";
        }
        return SUCCESS;
    }

    /**
     * 编辑操作
     * @return
     */
    public String processBusinessRequest()
    {
        User curUser = ApplicationContextUtil.getUserContextObject()
        .getCurrUser();
        Criterion cond = null;
        cond = Restrictions.gt("id", 0);
        nextTeps = nextTepService.findBy(cond);
        processConfigDetails = processConfigDetailService.findAll();
        // 当为新增流程时，初始化当前流程信息；否则为修改流程时，取流程信息；
        if (businessRequest != null && businessRequest.getId()!=null)
        {
            businessRequest = (BusinessRequest) businessRequestService
                    .findById(businessRequest.getId());
            init();
            cond = Restrictions.eq("refEntityId", businessRequest);
            List list = transactProcessStatusService.findBy(cond);
            if (list != null && list.size() > 0){
                transactProcessStatus = (P) list.get(0);
            }

            ///编辑前判断：判断当前办理人是自己的单才进入编辑页面，否则进入查看页面
            if(!curUser.getPerson().getName().equals(businessRequest.getCurrUser())){
                return "search";
            }
            
            //进展信息
            Criterion condt4 = Restrictions.eq("requestBusiness", businessRequest);
            Criterion condts4 = Restrictions.eq("recotdId", "00000");
            Criterion cond4 = Restrictions.and(condt4, condts4);
            //当前步骤进展信息
            Criterion cond6 = Restrictions.eq("processConfigDetail", transactProcessStatus.getCurrProcessConfigDetail());
            Criterion pcdcont = Restrictions.and(cond4, cond6);
            reqProgressess = reqProgressService.findBy(pcdcont);
            //当前步骤延迟信息
            reqDelayss = reqProgressService.findByrd(pcdcont);
            
            //历史进展信息
            if(procTranRecords != null && procTranRecords.size() > 0){
                for (Iterator iterator = procTranRecords.iterator(); iterator.hasNext();)
                {
                    ProcTranRecord temrp = (ProcTranRecord) iterator.next();
                    //关联流程节点步骤
                    Criterion cond5 = Restrictions.eq("processConfigDetail", temrp.getProcessStep());
                    Criterion cond7 = Restrictions.eq("recotdId", temrp.getId().toString());
                    Criterion condtemp = Restrictions.and(condt4, cond5);
                    Criterion condtemp1 = Restrictions.and(condtemp, cond7);
                    
                    List<ReqProgress> tempes = reqProgressService.findBy(condtemp1);
                    temrp.setReqProgresses(tempes);
                }
            }
            //历史延迟信息
            if(procTranRecords != null && procTranRecords.size() > 0){
                for (Iterator iterator = procTranRecords.iterator(); iterator.hasNext();)
                {
                    ProcTranRecord temrp = (ProcTranRecord) iterator.next();
                    //关联流程节点步骤
                    Criterion cond5 = Restrictions.eq("processConfigDetail", temrp.getProcessStep());
                    Criterion cond7 = Restrictions.eq("recotdId", temrp.getId().toString());
                    Criterion condtemp = Restrictions.and(condt4, cond5);
                    Criterion condtemp1 = Restrictions.and(condtemp, cond7);
                    List<ReqDelay> tempes = reqProgressService.findByrd(condtemp1);
                    temrp.setReqDelayes(tempes);
                }
            }
            
            /**进展信息附件
            if(reqProgresses != null && reqProgresses.size() > 0){
                for (Iterator iterator = reqProgresses.iterator(); iterator.hasNext();)
                {
                    ReqProgress temrp = (ReqProgress) iterator.next();
                    Criterion condtemp = Restrictions.eq("reqProgress", temrp);
                    temrp.setReqProAttaches(reqProgressService.findAttachBy(condtemp));
                }
            }
            **/
        }
        else
        {
            businessRequest = new BusinessRequest();
            businessRequest.setStartPerson(curUser.getPerson().getName());
            String pID=curUser.getPersonID();
            //查找所在部门
            Criterion cond1 = Restrictions.eq("person", curUser.getPerson());  
            List<OrgRelaPerson> orps=orgRelaPersonService.findBy(cond1);
            if (orps!=null&&orps.size()>0){
                OrgRelaPerson orp=orps.get(0);
                businessRequest.setStartDept(orp.getOrganization().getName());
            }
            // 新增流程时，初始化信息：状态为开始、当前办理人为操作人，
            transactProcessStatus = (P) new TransactProcessStatus();
            //新增产品显示默认流程：1，改进产品为非默认流程：0
            Criterion cond5 = Restrictions.eq("isDefault", "0" );
            if(newprodReq!=null&&newprodReq.getId()>0){
                cond5 = Restrictions.eq("isDefault", "1" );
            }
            
            processConfigs = processConfigService.findBy(cond5); // 所属流程信息
            if (curUser != null)
            {
                transactProcessStatus.setCurrTransactor(curUser.getPerson().getName());
            }
        }

        return SUCCESS;
    }
    
    /**
     * 产品需求单启动
     * TODO 请输入方法说明
     * @return
     */
    public String startupNewprodReq(){
        // 获取附件是否为发布
        String[] attachIDs = getContextObjects("attachIDs");
        // 获取附件类型
        String [] fileTypes = getContextObjects("filetype");
        // 删除附件
        String[] delIDs = getContextObjects("delIDs");
        
        // 保存产品资料数据
        if(newprodReq!=null){
            if(newprodReq.getStartPerson()!=null&&newprodReq.getStartPerson().getId()!=null){
                newprodReq.setStartDept(getOrgByPerson(newprodReq.getStartPerson()));
            }else{
                newprodReq.setStartPerson(null);
                newprodReq.setStartDept(null);
            }
            if(newprodReq.getProdPerson()!=null&&newprodReq.getProdPerson().getId()!=null){
                newprodReq.setProdDept(getOrgByPerson(newprodReq.getProdPerson()));
            }else{
                newprodReq.setProdPerson(null);
                newprodReq.setProdDept(null);
            }

            newprodReqService.saveNewprodReq(newprodReq, uploads, fileNames,fileTypes, delIDs);

            Criterion cond1 = Restrictions.eq("refEntity", newprodReq
                    .getClass().getName());
            Criterion cond2 = Restrictions.eq("refEntityId", newprodReq.getId()
                    + "");
            Criterion cond3 = Restrictions.isNotNull("docType");
            Criterion cond4 = Restrictions.and(cond1, cond2);
            Criterion cond6 = Restrictions.and(cond4, cond3);
//            Criterion cond5 = Restrictions.isNotNull("docType");
//            Criterion condDocu = Restrictions.and(cond5, cond6);
            List<ReqDocu> reqDocus = reqDocuService.findBy(cond6);
            boolean flag=true;
            for(ReqDocu docu:reqDocus){
                if("COMPLAN".equals(docu.getDocType().getKeyValue())){
                    flag=false;
                }
            }
            if (flag)
            {
                this.returnMsg = "操作失败！要求提交附件不完整，不允许提交!";
                this.returnUrl = null;
                return "failure";
            }
            
        }
        
        prodReq=(ProdReq)newprodReqService.findById(newprodReq.getId());
        this.startPerson=prodReq.getStartPerson().getName();
        this.startDept=prodReq.getStartDept().getFullName();
        
        Criterion cond1 = Restrictions.eq("refEntity", "cn.ffcs.businessrequest.model.NewprodReq" );
        Criterion cond2 = Restrictions.eq("refEntityId", newprodReq.getId()+"");
        Criterion cond = Restrictions.and(cond1,cond2);
        reqDocus = reqDocuService.findBy(cond);
        
        
        return processBusinessRequest();
    }
    
    /**
     * 产品需求单启动
     * TODO 请输入方法说明
     * @return
     */
    public String startupProdAdvReq(){
        prodAdvReq=(ProdAdvReq) prodAdvReqService.findById(prodAdvReq.getId());
        
        Criterion cond1 = Restrictions.eq("refEntity", "cn.ffcs.businessrequest.model.ProdAdvReq" );
        ///判断是否合并单，是：则要将所有单的需求单号连接成新的需求单号；所有发起人为新的发起人；所有发起部门为新的发起部门
        if("HB".equals(prodAdvReq.getOpType())){
            String reqCode="";
            startPerson="";
            startDept="";
            Criterion cond = Restrictions.eq("childAreq", prodAdvReq.getId()+"" );
            List<ProdAdvReq> lists = prodAdvReqService.findBy(cond);
            for(ProdAdvReq tmp:lists){
                reqCode+=","+tmp.getReqCode();
                startPerson+=","+tmp.getStartPerson().getName();
                startDept+=","+tmp.getStartDept().getFullName();
                
                Criterion cond2 = Restrictions.eq("refEntityId", tmp.getId()+"");
                Criterion cond3 = Restrictions.and(cond1,cond2);
                if(reqDocus==null)
                    reqDocus = reqDocuService.findBy(cond3);
                else
                    reqDocus.addAll(reqDocuService.findBy(cond3));
            }
            if(startPerson!=null&&startPerson.length()>0){
                startPerson=startPerson.substring(1);
                startDept=startDept.substring(1);
            }
//            prodAdvReq.setReqCode(reqCode);
        }else{
            startPerson=prodAdvReq.getStartPerson().getName();
            startDept=prodAdvReq.getStartDept().getFullName();
            
            Criterion cond2 = Restrictions.eq("refEntityId", prodAdvReq.getId()+"");
            Criterion cond3 = Restrictions.and(cond1,cond2);
            reqDocus = reqDocuService.findBy(cond3);
        }
        
        prodReq=(ProdReq)prodAdvReq;
        return processBusinessRequest();
    }
    
    /**
     * 
     * TODO 请输入方法说明
     * @return
     */
    public String startupFlow(){
        ///需求单id关联到原产品需求单，关联需求单号，原需求单附件复制一份给新的需求单
        submitProcess();
        if(newprodReq!=null&&newprodReq.getId()!=null){
            ////原产品需求单结束，
            newprodReq=(NewprodReq)newprodReqService.findById(newprodReq.getId());
            businessRequest.setReqCode(newprodReq.getReqCode());
            newprodReq.setEnd(baseService.getCurrSysdate());
            newprodReqService.saveOrUpdate(newprodReq);
            
            reqDocuService.copyReqDocu(newprodReq.getClass().getName(), newprodReq.getId()+"", businessRequest);
            
        }else if(prodAdvReq!=null&&prodAdvReq.getId()!=null){
            prodAdvReq=(ProdAdvReq)prodAdvReqService.findById(prodAdvReq.getId());
            prodAdvReq.setTranStatus("PROD");
            prodAdvReq.setRelaReq(businessRequest);
            prodAdvReqService.saveOrUpdate(prodAdvReq);
            ///判断是否合并，合并的需要修改原单的关系需求单号信息
            if(prodAdvReq.getOpType()!=null&&prodAdvReq.getOpType().equals("HB")){
                Criterion cond1 = Restrictions.eq("childAreq", prodAdvReq.getId()+"");
                List<ProdAdvReq> prodAdvReqs = prodAdvReqService.findBy(cond1);
                for(ProdAdvReq tmp:prodAdvReqs){
                    tmp.setRelaReq(businessRequest);
                    prodAdvReqService.saveOrUpdate(tmp);
                    reqDocuService.copyReqDocu(tmp.getClass().getName(), tmp.getId()+"", businessRequest);
                }
            }else{
                reqDocuService.copyReqDocu(prodAdvReq.getClass().getName(), prodAdvReq.getId()+"", businessRequest);
            }
        }
        
        return SUCCESS;
    }
    
    public String saveTransactProcessStatus(){
        if(prodAdvReq!=null&&prodAdvReq.getId()>0&&businessRequest!=null&&businessRequest.getId()>0){
            prodAdvReq=(ProdAdvReq)prodAdvReqService.findById(prodAdvReq.getId());
            this.saveOrUpdate();
            prodAdvReq.setRelaReq(businessRequest);
        }
        return SUCCESS;
    }
    
    /**
     * 
     * 判断当前流程是否有多个用户在处理
     * @return
     */
    public boolean isTransactors(TransactProcessStatus transactProcessStatus){
        if(transactProcessStatus!=null&&transactProcessStatus.getId()!=null){
            Criterion cond1 = null;
            cond1 = Restrictions.eq("currProcess", transactProcessStatus);
            Criterion conds = Restrictions.and(cond1, Restrictions.eq("tranType", "1"));
            List<Transactor> list=transactorService.findBy(conds);
            if(list!=null&&list.size()>=2){
                return true;
            }
        }
        return false;
    }
    
    /**
     * 
     * 根据办理人类型获取办理人
     * @return
     */
    public String viewTransactorByType()
    {
        List list = null;
        if (super.getConds() != null)
        {
            String id = super.getConds().get("id") + "";
            String businessRequestId=super.getConds().get("businessRequestId") + "";
            if (!"".equals(id))
            {
                ProcessConfigDetail processConfigDetail = (ProcessConfigDetail) processConfigDetailService
                        .findById(new Integer(id));
                businessRequest=(BusinessRequest)businessRequestService.findById(new Integer(businessRequestId));
                Criterion cond1 = Restrictions.eq("refEntity", businessRequest.getClass().getName());
                Criterion cond2 = Restrictions.eq("orderBy", 1);
                Criterion cond3 = Restrictions.eq("refEntityId",businessRequest);
                Criterion condProcTranRecord = Restrictions.and(cond1, cond2);
                condProcTranRecord = Restrictions.and(condProcTranRecord, cond3);
                procTranRecords = procTranRecordService.findByAndOrder(condProcTranRecord, "id", "desc");
                Person person=null;
                if(procTranRecords!=null&&procTranRecords.size()>0){
                    person=procTranRecords.get(0).getTransactor();
                }
                list = findListData(processConfigDetail.getTransactorType(),person);
            }
        }
        ActionContext ctx = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse) ctx
                .get(ServletActionContext.HTTP_RESPONSE);
        String xml = MyList.getAll(list);
        response.setCharacterEncoding("gb2312");// 页面编码设置
        response.setContentType("text/xml");// 页面文件头格式
        // System.out.println(xml);
        try
        {
            response.getWriter().write(xml);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public List findListData(TransactorType transactorType,Person person)
    {
        if (transactorType != null && transactorType.getTaskRealize() != null
                && transactorType.getMethodName() != null
                && transactorType.getParam() != null)
        {
            String[] params = transactorType.getParam().split(",");
            GetSet getSet = new GetSet();
            try
            {
                if("preTransactor".equals(transactorType.getParam())){
                    List<FieldObject> result=new ArrayList<FieldObject>();
                    FieldObject fo=new FieldObject();
                    fo.setName(person.getName());
                    fo.setId(person.getId());
                    result.add(fo);
                    return result;
                }else{
                    return (List) getSet.getMethod(Class.forName(
                            transactorType.getTaskRealize()).newInstance(),
                            transactorType.getMethodName(), params);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return new ArrayList();
    }


    /**
     * 需求延迟
     * @param baseEntity
     */
    public String viewReqDelay()
    {

        ActionContext ctx = ActionContext.getContext();        
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        HttpServletRequest request =    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        String type=request.getParameter("type");//radio,checkbox,null
        String module=request.getParameter("module");//organ,person
        Criterion cond = Restrictions.eq("refEntityId", businessRequest);
        List list = transactProcessStatusService.findBy(cond);
        if (list != null && list.size() > 0){
            transactProcessStatus = (P) list.get(0);
        }
        returnMsg = "view";
        if(businessRequest!=null&&businessRequest.getId()!=null){

        }
        
        return SUCCESS;
    }

    

    /**
     * 新增需求延迟
     * @param baseEntity
     */
    public String saveReqDelay()
    {
        User currUser=ApplicationContextUtil.getUserContextObject().getCurrUser();
        reqDelay.setCreateTime(new Date());
        reqDelay.setCreator(currUser.getCreator());
        reqDelay.setRequestBusiness(businessRequest);
        reqDelay.setRecotdId("00000");
        boolean isSave = false;
        Criterion cond = Restrictions.eq("refEntityId", businessRequest);
        List list = transactProcessStatusService.findBy(cond);
        if (list != null && list.size() > 0){
            transactProcessStatus = (P) list.get(0);
        }
        reqDelay.setProcessConfigDetail(transactProcessStatus.getCurrProcessConfigDetail());
        isSave = reqProgressService.saveReqDelay(reqDelay);
        //修改办理时限
        transactProcessStatus.setTimeLimit(
                DateUtils.addDays(
                        transactProcessStatus.getTimeLimit() == null? new Date() : transactProcessStatus.getTimeLimit(),
                        Integer.valueOf(reqDelay.getDelayTime().trim())*7));
        transactProcessStatusService.saveOrUpdate(transactProcessStatus);
        if(isSave){
            returnMsg = "保存成功！";
        }else{
            returnMsg = "保存失败！";
        }
        return SUCCESS;
    }


    /**
     * 需求进展情况
     * @param baseEntity
     */
    public String viewReqProgress()
    {

        ActionContext ctx = ActionContext.getContext();        
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        HttpServletRequest request =    (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
        String type=request.getParameter("type");//radio,checkbox,null
        String module=request.getParameter("module");//organ,person
        returnMsg = "view";
        if(businessRequest!=null&&businessRequest.getId()!=null){

        }
        
        return SUCCESS;
    }

    /**
     * 新增需求进展情况
     * @param baseEntity
     */
    public String saveReqProgress()
    {
        User currUser=ApplicationContextUtil.getUserContextObject().getCurrUser();
        reqProgress.setCreateTime(new Date());
        reqProgress.setCreator(currUser.getCreator());
        reqProgress.setRequestBusiness(businessRequest);
        reqProgress.setRecotdId("00000");
        Criterion cond = Restrictions.eq("refEntityId", businessRequest);
        List list = transactProcessStatusService.findBy(cond);
        if (list != null && list.size() > 0){
            transactProcessStatus = (P) list.get(0);
        }
        reqProgress.setProcessConfigDetail(transactProcessStatus.getCurrProcessConfigDetail());
        boolean isSave = false;
        isSave = reqProgressService.saveReqProgress(reqProgress, uploadspro);
        if(isSave){
            returnMsg = "保存成功！";
        }else{
            returnMsg = "保存失败！";
        }
        return SUCCESS;
    }

    
    /**
     * 新增或修改操作
     * @param baseEntity
     */
    public String saveOrUpdate()
    {
        boolean flag=true;      //新增标识，true为新增，false为修改
        // 获取附件是否为发布
        String[] publishIDs = getContextObjects("publishIDs");
        String[] reqDocuIDs = getContextObjects("reqDocuIDs");
        // 删除附件
        String[] delIDs = getContextObjects("delIDs");
        User curUser = ApplicationContextUtil.getUserContextObject().getCurrUser();
        if(businessRequest!=null&&businessRequest.getId()!=null){
            flag=false;
        }
        else{
            if (curUser != null)
            {
                transactProcessStatus.setCurrTransactor(curUser.getPerson().getName());
            }
        }

        if(businessRequest.getProdPerson()!=null&&businessRequest.getProdPerson().getId()!=null){
            businessRequest.setProdDept(getOrgByPerson(businessRequest.getProdPerson()));
        }else{
            businessRequest.setProdPerson(null);
            businessRequest.setProdDept(null);
        }
        // 保存数据
        businessRequestService.saveBusinessRequest(businessRequest, uploads,
                fileNames, publishIDs, reqDocuIDs, delIDs);
        transactProcessStatus.setRefEntityId(businessRequest);
        transactProcessStatus.setRefEntity(businessRequest.getClass().getName());

        baseService.saveOrUpdate(transactProcessStatus);
        if(flag){       //新增的时候，保存当前操作人为待办人;修改时不处理
            Transactor transactor = new Transactor();
            transactor.setTranType("1");
            transactor.setCurrProcess(transactProcessStatus);
            Person person = new Person();
            person.setId(new Integer(curUser.getPersonID()));
            transactor.setUserId(person);
            transactorService.save(transactor);
            ///将已上传的附件，关联到需求单
            if(documentIds!=null&&documentIds.length()>1){
                String [] docIds=documentIds.substring(1).split(",");
                ReqDocu reqDocu;
                for(String i:docIds){
                    reqDocu=(ReqDocu)reqDocuService.findById(new Integer(i));
                    reqDocu.setRefEntityId(businessRequest.getId()+"");
                    reqDocuService.update(reqDocu);
                }
                
            }
            //处理需求单号  新增时添加，修改时不添加
           // String requestCode = this.getReqCode();
           // businessRequest.setReqCode(requestCode);
        }
        
        return processBusinessRequest();
    }
    
    public Organization getOrgByPerson(Person person ){
        if (person!=null&&person.getId()>0){
            return orgRelaPersonService.findByPageByPersonId(person.getId()+"");
        }
        return null;
    }
    
    /**
     * 提交流程申请
     * @param baseEntity
     */
    public String submitProcess()
    {
        if(businessRequest!=null&&businessRequest.getId()!=null){
            setRequestParam("businessRequestId", businessRequest.getId()+"");
        }
        User curUser=ApplicationContextUtil.getUserContextObject().getCurrUser();
        if (transactProcessStatus == null||transactProcessStatus.getCurrTransactor()==null||curUser==null){
            this.returnMsg="操作失败！";
            this.returnUrl=null;
            return SUCCESS;
        }
        if("ZHP".equals(getContextObject("nextStep"))){ //转派操作，变更下一办理人
            String nextPerson = getContextObject("nextPerson");
            if(nextPerson!=null&&!nextPerson.equals("")&&transactProcessStatus!=null&&transactProcessStatus.getId()!=null){
                String nextPersonName = getContextObject("nextPersonName");
                String info=getContextObject("transactorInfo");
                updateCurperson(nextPerson,nextPersonName);
                fillProcTranRecord(1, "转派意见："+info);
                this.returnMsg="转派操作成功！";
                return SUCCESS;
            }
            this.returnMsg="转派操作失败！";
            return "failure";
        }

        // 获取附件是否为发布
        String[] publishIDs = getContextObjects("publishIDs");
        String[] ReqDocuIDs = getContextObjects("ReqDocuIDs");
        // 删除附件
        String[] delIDs = getContextObjects("delIDs");

        if(businessRequest.getProdPerson()!=null&&businessRequest.getProdPerson().getId()!=null){
            businessRequest.setProdDept(getOrgByPerson(businessRequest.getProdPerson()));
        }else{
            businessRequest.setProdPerson(null);
            businessRequest.setProdDept(null);
        }
        // 保存数据
        businessRequestService.saveBusinessRequest(businessRequest, uploads,
                fileNames, publishIDs, ReqDocuIDs, delIDs);
        
        ///将已上传的附件，关联到需求单
        if(documentIds!=null&&documentIds.length()>1){
            String [] docIds=documentIds.substring(1).split(",");
            ReqDocu reqDocu;
            for(String i:docIds){
                reqDocu=(ReqDocu)reqDocuService.findById(new Integer(i));
                reqDocu.setRefEntityId(businessRequest.getId()+"");
                reqDocuService.update(reqDocu);
            }
        }

        // 提交申请后，当前流程信息改为前一流程信息，前一流程信息入历史表
        transactProcessStatus.setRefEntityId(businessRequest);
        transactProcessStatus
                .setRefEntity(businessRequest.getClass().getName());
        ///当前代办人为多人时，为会签办理，只有当最后一个人提交的时候，流程才转入下一步骤
        boolean nextFlag=true;
//        nextFlag=!isTransactors(transactProcessStatus);
        
        if(nextFlag){
            ////需求单提交后，判断必须提交的文件是否都提交了，
            Criterion cond = Restrictions.eq("processDetail", transactProcessStatus.getCurrProcessConfigDetail());
            documentTypes = documentTypeService.findBy(cond);
            if(documentTypes!=null&&documentTypes.size()>0){
                Criterion cond1 = Restrictions.eq("refEntity", new BusinessRequest().getClass().getName());
                Criterion cond2 = Restrictions.eq("refEntityId", businessRequest.getId()+"");
                Criterion cond3 = Restrictions.eq("processDetail", transactProcessStatus.getCurrProcessConfigDetail());
                Criterion cond4 = Restrictions.and(cond1, cond2);
                Criterion cond6 = Restrictions.and(cond4, cond3);
                Criterion cond5 = Restrictions.isNotNull("docType");
                Criterion condDocu = Restrictions.and(cond5, cond6);
                List<ReqDocu> reqDocus= reqDocuService.findBy(condDocu);
                if(reqDocus==null||documentTypes.size()!=reqDocus.size()){
                    this.returnMsg="操作失败！要求提交附件不完整，不允许提交!";
                    this.returnUrl=null;
                    return SUCCESS;
                }
            }
        }
        
        fillProcTranRecord(1, "");
        if(nextFlag){
            toNextConfig();
            if(transactProcessStatus.getCurrProcessConfigDetail()!=null){
                ProcessConfigDetail detail=(ProcessConfigDetail)processConfigDetailService.findById(transactProcessStatus.getCurrProcessConfigDetail().getId());
                try
                {
                    int timeLimit=new Integer(detail.getTimeLimit());
                    if(timeLimit>0){
                        transactProcessStatus.setTimeLimit(DateConverter.computeDate(baseService.getCurrSysdate(),timeLimit));
                    }
                }
                catch (NumberFormatException e)
                {
                }
            }
            transactProcessStatus.setBegin(baseService.getCurrSysdate());
            businessRequest.setCurrStatus(transactProcessStatus
                    .getCurrProcessConfigDetail().getName());
            businessRequest.setCurrUser(transactProcessStatus
                    .getCurrTransactor());
            businessRequestService.saveOrUpdate(businessRequest);
            
            transactProcessStatus.setRefEntityId(businessRequest);
            baseService.saveOrUpdate(transactProcessStatus);
        }
        if(transactProcessStatus!=null&&transactProcessStatus.getId()!=null){
            HashMap<String, Object> conds=new HashMap();
            conds.put("currProcess",transactProcessStatus.getId() + "");
            conds.put("userId",curUser.getPersonID() + "");
            
            transactorService.remove(null, conds);
        }
        if(!nextFlag){
            this.returnMsg="操作成功！<br><br>流程已成功提交！";
            return SUCCESS;
        }
        
        String nextPerson = getContextObject("nextPerson");
        
        if (transactProcessStatus.getCurrTransactor() != null)
        {
            if (nextPerson == null || "".equals(nextPerson))
            {
                nextPerson = curUser.getPersonID();
            }

            Transactor transactor = new Transactor();
            transactor.setTranType("1");
            transactor.setCurrProcess(transactProcessStatus);
            Person person = new Person();
            person.setId(new Integer(nextPerson));
            transactor.setUserId(person);
            transactorService.saveOrUpdate(transactor);
        }

        this.returnMsg="操作成功！<br><br>流程已成功提交！";
        if(businessRequest!=null&&businessRequest.getId()!=null){
            this.returnUrl="/bpm/processBusinessRequest.action?businessRequest.id="+businessRequest.getId();
        }
        return SUCCESS;
    }
    

    // 将前一流程信息填写入历史表
    public void fillProcTranRecord(int orderby, String transactorInfo)
    {
        procTranRecord = new ProcTranRecord();
        procTranRecord.setOrderBy(orderby);
        procTranRecord.setRefEntity(transactProcessStatus.getRefEntity());
        procTranRecord.setRefEntityId(transactProcessStatus.getRefEntityId());
        procTranRecord.setProcessStep(transactProcessStatus
                .getCurrProcessConfigDetail());
        procTranRecord.setProcessConfig(transactProcessStatus
                .getCurrProcessConfig());
        Person person = new Person();
        person.setId(new Integer(ApplicationContextUtil.getUserContextObject()
                .getCurrUser().getPersonID()));
        procTranRecord.setTransactor(person);
        if ("".equals(transactorInfo))
        {
            procTranRecord.setTransactInfo(getContextObject("transactorInfo"));
        }
        else
        {
            procTranRecord.setTransactInfo(transactorInfo);
        }
        procTranRecord.setTransactTime(baseService.getCurrSysdate());
        procTranRecordService.saveOrUpdate(procTranRecord);

        //修改 进展信息 历史记录ID
        Criterion cond1 = Restrictions.eq("requestBusiness", businessRequest);
        Criterion cond2 = Restrictions.eq("recotdId", "00000");
        Criterion condtemp = Restrictions.and(cond1, cond2);

        List<ReqProgress> tempes = reqProgressService.findBy(condtemp);
        
        if(tempes != null && tempes.size() > 0){
            for (Iterator iterator = tempes.iterator(); iterator.hasNext();)
            {
                ReqProgress reqProgress = (ReqProgress) iterator.next();
                ReqProgress reqProgresstemp = reqProgress;
                reqProgresstemp.setRecotdId(procTranRecord.getId().toString());
                reqProgressService.updateReqProgress(reqProgresstemp);
            }
        }
        //修改 延迟信息 历史记录ID
        Criterion cond3 = Restrictions.eq("requestBusiness", businessRequest);
        Criterion cond4 = Restrictions.eq("recotdId", "00000");
        Criterion condtemp1 = Restrictions.and(cond3, cond4);
        List<ReqDelay> tempes1 = reqProgressService.findByrd(condtemp1);
        
        if(tempes1 != null && tempes1.size() > 0){
            for (Iterator iterator = tempes1.iterator(); iterator.hasNext();)
            {
                ReqDelay reqDelay = (ReqDelay) iterator.next();
                ReqDelay ReqDelaytemp = reqDelay;
                ReqDelaytemp.setRecotdId(procTranRecord.getId().toString());
                reqProgressService.updateReqDelay(ReqDelaytemp);
            }
        }
        
    }

    // 将当前流程信息替换前一流程信息,下一步骤信息写入当前流程信息
    public void toNextConfig()
    {
        transactProcessStatus.setPreProcessConfig(transactProcessStatus
                .getCurrProcessConfig());
        transactProcessStatus.setPreProcessConfigDetail(transactProcessStatus
                .getCurrProcessConfigDetail());
        transactProcessStatus.setPreProcessStep(transactProcessStatus
                .getCurrProcessStep());
        transactProcessStatus.setPreTransactor(transactProcessStatus
                .getCurrTransactor());

        String processConfigDetailId = getContextObject("nextStep");

        ProcessConfigDetail currProcessConfigDetail = (ProcessConfigDetail) processConfigDetailService
                .findById(new Integer(processConfigDetailId));
        transactProcessStatus
                .setCurrProcessConfigDetail(currProcessConfigDetail);
        if (currProcessConfigDetail == null
                || "1".equals(currProcessConfigDetail.getNodeType()))
        {
            transactProcessStatus.setCurrTransactor(null);
        }
        else if (getContextObject("nextPersonName") != null
                && !"".equals(getContextObject("nextPersonName")))
        {
            transactProcessStatus
                    .setCurrTransactor(getContextObject("nextPersonName"));
        }
        else
        {
            transactProcessStatus.setCurrTransactor(ApplicationContextUtil
                    .getUserContextObject().getCurrUser().getUserID());
        }
    }

//    // 上一个流程节点填入当前节点；
//    public void toPreConfig()
//    {
//        transactProcessStatus.setPreProcessConfig(transactProcessStatus
//                .getCurrProcessConfig());
//        transactProcessStatus.setPreProcessConfigDetail(transactProcessStatus
//                .getCurrProcessConfigDetail());
//        transactProcessStatus.setPreProcessStep(transactProcessStatus
//                .getCurrProcessStep());
//        transactProcessStatus.setPreTransactor(transactProcessStatus
//                .getCurrTransactor());
//
//        transactProcessStatus.setCurrProcessConfig(transactProcessStatus
//                .getPreProcessConfig());
//        transactProcessStatus.setCurrProcessConfigDetail(transactProcessStatus
//                .getPreProcessConfigDetail());
//        transactProcessStatus.setPreProcessStep(transactProcessStatus
//                .getCurrProcessStep());
//        transactProcessStatus.setCurrTransactor(transactProcessStatus
//                .getPreTransactor());
//    }

    /**
     * 流程申请回退
     * @param baseEntity
     */
    public String backProcess()
    {
        backToPre("回退意见："+advice);
        return SUCCESS;
    }

    /**
     * 流程申请回收 要求办理人是当前操作人，当前节点是开始节点的，才允许回收 删除流程办理信息、办理人信息、增加一条回收的历史记录
     */
    public String removeProcess(){
        backToPre("回收");
        processBusinessRequest();
        return SUCCESS;
    }
    
    /**
     * 流程申请删除
     */
    public String deleteProcess()
    {
        if (businessRequest != null&&businessRequest.getId()!=null)
        {
            Criterion cond1 = Restrictions.eq("refEntity", businessRequest.getClass().getName());
            Criterion cond2 = Restrictions.eq("refEntityId", businessRequest);
            Criterion conds = Restrictions.and(cond1, cond2);
            List<TransactProcessStatus> list=transactProcessStatusService.findBy(conds);
            if(list!=null){
                for(int i=0;i<list.size();i++){
                    transactProcessStatus=(P)list.get(i);
                    HashMap<String, Object> condTemp= super.getConds();
                    if (condTemp==null) {
                        super.setConds(new HashMap<String, Object>());
                    }
                    super.getConds().put("currProcess",transactProcessStatus.getId() + "");
                    transactorService.remove(null, super.getConds());
                    fillProcTranRecord(0, "删除");
                    transactProcessStatusService.remove(transactProcessStatus);
                }
            }
            businessRequestService.remove(businessRequest);
        }
        return SUCCESS;
    }

    /**
     * 批量删除
     * @return
     */
    public String batchDel()
    {
        String[] ids = getContextObjects("ids");
        if(ids!=null&&ids.length>0){
            if(businessRequest==null)
                businessRequest=new BusinessRequest();
            for(int i=0;i<ids.length;i++){
                businessRequest.setId(new Integer(ids[i]));
                this.deleteProcess();
            }
        }
        returnMsg = "批量删除操作已成功！";
        return SUCCESS;
    }
    
    public String backToPre(String info)
    {
        if (transactProcessStatus == null
                || transactProcessStatus.getId() == null
                || "".equals(transactProcessStatus.getId()))
        {
            return SUCCESS;
        }
        transactProcessStatus = (P) transactProcessStatusService
                .findById(new Integer(transactProcessStatus.getId()));
        // /当前节点为开始节点时，不处理回退操作；否则转上一步骤
        if ("0".equals(transactProcessStatus.getCurrProcessConfigDetail()
                .getNodeType()))
        {
            return SUCCESS;
        }
        // 判断上一个流程节点不为空时，当前节点入历史表，上一个流程节点填入当前节点；
        if (transactProcessStatus.getPreProcessConfigDetail() != null
                && !""
                        .equals(transactProcessStatus
                                .getPreProcessConfigDetail()))
        {
            // 从历史记录中恢复流程信息
            if (businessRequest != null)
            {
                businessRequest=(BusinessRequest)businessRequestService.findById(businessRequest.getId());
                Criterion cond1 = Restrictions.eq("refEntity", businessRequest
                        .getClass().getName());
                Criterion cond2 = Restrictions.eq("orderBy", 1);
                Criterion cond3 = Restrictions.eq("refEntityId",
                        businessRequest);
                Criterion condProcTranRecord = Restrictions.and(cond1, cond2);
                condProcTranRecord = Restrictions
                        .and(condProcTranRecord, cond3);
                procTranRecords = procTranRecordService.findByAndOrder(
                        condProcTranRecord, "id", "desc");
                fillProcTranRecord(0, info);
                if (procTranRecords != null)
                {
                    if (procTranRecords.size() > 0
                            && procTranRecords.get(0).getTransactor() != null)
                    {
                        transactProcessStatus
                                .setCurrProcessConfig(transactProcessStatus
                                        .getPreProcessConfig());
                        transactProcessStatus
                                .setCurrProcessConfigDetail(transactProcessStatus
                                        .getPreProcessConfigDetail());
                        transactProcessStatus
                                .setCurrTransactor(transactProcessStatus
                                        .getPreTransactor());
                        transactProcessStatus
                                .setPreProcessStep(transactProcessStatus
                                        .getPreProcessStep());
                        procTranRecord = procTranRecords.get(0);
                        
                        transactorService.saveTransactor(procTranRecord
                                .getTransactor().getId()
                                + "", "1", transactProcessStatus);
                        procTranRecord.setOrderBy(0);
                        procTranRecordService.saveOrUpdate(procTranRecord);
                        if (procTranRecords.size() > 1
                                && procTranRecords.get(1) != null)
                        {
                            procTranRecord = procTranRecords.get(1);
                        }
                        else
                        {
                            procTranRecord = new ProcTranRecord();
                        }

                        transactProcessStatus
                                .setPreProcessConfig(procTranRecord
                                        .getProcessConfig());
                        transactProcessStatus
                                .setPreProcessConfigDetail(procTranRecord
                                        .getProcessStep());
                        if (procTranRecord.getTransactor() != null)
                            transactProcessStatus
                                    .setPreTransactor(procTranRecord
                                            .getTransactor().getName());
                        else
                            transactProcessStatus.setPreTransactor(null);
                        transactProcessStatusService
                                .saveOrUpdate(transactProcessStatus);
                        businessRequest.setCurrUser(transactProcessStatus.getCurrTransactor());
                        businessRequest.setCurrStatus(transactProcessStatus.getCurrProcessConfigDetail().getName());
                        businessRequestService.saveOrUpdate(businessRequest);
                    }
                }
            }
        }
        return SUCCESS;
    }

    public List offset(List list,int pageNo,int pageSize){
        int offset=0;
        int subNo=0;
        
        if(list.size()>=pageNo*pageSize){
            offset=(pageNo-1)*pageSize;
        }else{
            offset=list.size()/pageSize*pageSize;
        }
        if(list.size()>pageNo*pageSize){
            subNo=offset+pageSize;
        }else{
            subNo=list.size();
        }
        return list.subList(offset,subNo);
    }
    
    public String toSuccess(){
        return SUCCESS;
    }
    
    public String setRequestParam(String name,String param){
        ActionContext ctx = ActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) ctx
                .get(ServletActionContext.HTTP_REQUEST);
        request.setAttribute(name, param);
        return SUCCESS;
    }

    /**
     * 
     * TODO 请输入方法说明
     */
    private void updateCurperson(String nextPerson,String nextPersonName){
        if(nextPerson!=null&&!nextPerson.equals("")&&transactProcessStatus!=null&&transactProcessStatus.getId()!=null){
            ///转派更新当前办理人：更新待办人表，更新待办表当前办理人，更新需求单当前办理人
            transactProcessStatus=baseService.findById(transactProcessStatus.getId());
            transactProcessStatus.setPreTransactor(transactProcessStatus.getCurrTransactor());
            transactProcessStatus.setCurrTransactor(nextPersonName);
            baseService.saveOrUpdate(transactProcessStatus);
            businessRequest=(BusinessRequest)businessRequestService.findById(businessRequest.getId());
            businessRequest.setCurrUser(nextPersonName);
            businessRequestService.saveOrUpdate(businessRequest);

            HashMap<String, Object> conds=new HashMap();
            conds.put("currProcess",transactProcessStatus.getId() + "");
            transactorService.remove(null, conds);

            Transactor transactor = new Transactor();
            transactor.setTranType("1");
            transactor.setCurrProcess(transactProcessStatus);
            Person person = new Person();
            person.setId(new Integer(nextPerson));
            transactor.setUserId(person);
            transactorService.saveOrUpdate(transactor);
        }
        return ;
    }

    /**
     * 删除操作
     * @return
     */
    public String remove()
    {
        baseService.remove(transactProcessStatus);
        return SUCCESS;
    }

    /**
     * 需求单报表页面
     * @return
     */
    public String toProdAdvReqForm()
    {
        ActionContext ctx = ActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) ctx
                .get(ServletActionContext.HTTP_REQUEST);
        HttpSession session=request.getSession();
        session.setAttribute("businessRequests", null);
        session.setAttribute("processConfigDetails", null);
        
        initForm();
        if(pageControl==null){
        	pageControl=new PageControl(0,1,10);
        }
        return SUCCESS;
    }
    
    /**
     * 报表信息初始化
     * @return
     */
    public void initForm(){
//        Criterion cond5 = Restrictions.eq("relaReq", businessRequest);
        processConfigs=processConfigService.findAll();
    }
    
    /**
     * 需求单报表查询
     * @return
     */
    public String queryProdAdvReqForm()
    {
        ActionContext ctx = ActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        HashMap<String, Object> conds=super.getConds();
        if(conds==null){
        	conds=new HashMap<String, Object>();
        }
        String pconfigId=(String)conds.get("procTranRecord.processConfig");
        Criterion cond1 = Restrictions.eq("nodeType", "0");
        Criterion cond2 = Restrictions.eq("processConfig", new ProcessConfig(new Integer(pconfigId)));
        Criterion condProcTranRecord = Restrictions.and(cond1, cond2);
        processConfigDetails = processConfigDetailService.findBy(condProcTranRecord);
        request.getSession().setAttribute("processConfigDetails", processConfigDetails);
        Hashtable hash=new Hashtable();
        if(processConfigDetails!=null&&processConfigDetails.size()>0){
            List<NextTep> tmps=nextTepService.find(processConfigDetails.get(0).getId());
            processConfigDetails=new ArrayList<ProcessConfigDetail>();
            for(NextTep tmp:tmps){
                if(hash.containsKey(tmp.getProcessDetail().getId())){
                    //processConfigDetails.remove(hash.get(tmp.getProcessDetail().getId()));
                    //hash.remove(tmp.getProcessDetail().getId());
                }else{
                	processConfigDetails.add(tmp.getProcessDetail());
                    hash.put(tmp.getProcessDetail().getId(),tmp.getProcessDetail());
                }
                
            }
            processConfigDetails.add(tmps.get(tmps.size()-1).getNextConfig());
            num=processConfigDetails.size();
        }
        

        initForm();
        businessRequests=businessRequestService.findOrganized(conds);
        request.getSession().setAttribute("businessRequests", businessRequests);
        if(pageControl==null){
            pageControl=new PageControl(businessRequests.size(),1,10,0);
        }
        else{
            pageControl=new PageControl(businessRequests.size(),pageControl.getPageNo(),10,0);
        }
        List<BusinessRequest> brs=this.offset(businessRequests, pageControl.getPageNo(), pageControl.getPageSize());
        if(brs!=null){
            for(BusinessRequest br:brs){
                List<ProcTranRecord> rtrs=br.getProcTranRecords();
                hash=new Hashtable();
                for(ProcTranRecord rtr:rtrs){
                    if(rtr!=null)
                        hash.put(rtr.getProcessStep().getId(), rtr.getProcessStep().getId());
                }
                
                ////获取当前步骤加入流程情况描述
                Criterion cond3 = Restrictions.eq("refEntityId", br);
                List<TransactProcessStatus> tpss = transactProcessStatusService.findBy(cond3);
                TransactProcessStatus transactProcessStatus;
                if(tpss!=null&&tpss.size()>0){
                    transactProcessStatus=tpss.get(0);
                    if(transactProcessStatus!=null&&transactProcessStatus.getCurrProcessConfigDetail()!=null)
                        hash.put(transactProcessStatus.getCurrProcessConfigDetail().getId(), transactProcessStatus.getCurrProcessConfigDetail().getId());
                    
                    //当前步骤进展信息
                    Criterion condt4 = Restrictions.eq("requestBusiness", br);
                    Criterion condts4 = Restrictions.eq("recotdId", "00000");
                    Criterion cond4 = Restrictions.and(condt4, condts4);
                    Criterion cond6 = Restrictions.eq("processConfigDetail", transactProcessStatus.getCurrProcessConfigDetail());
                    Criterion pcdcont = Restrictions.and(cond4, cond6);
                    List reqProgresses = reqProgressService.findBy(pcdcont);
                    br.setReqProgresses(reqProgresses);
                }
                float f=0;
                List temp=new ArrayList();
                for(ProcessConfigDetail pcd:processConfigDetails){
                    FieldObject fo=new FieldObject();
                    if(hash.containsKey(pcd.getId())){
                        fo.setId(1);    ////存在当前节点
                        temp.add(fo);
                        f=0;
                    }else{
                        fo.setId(0);    ////不存在当前节点
                        temp.add(fo);
                        f++;
                    }
                }
                f=100-f/num*100;
                if(f>5){
                    f=f-4;
                }
                br.setPercent((int)f);
                br.setDisplay(temp);
    
            }
        }
        
        pageControl.setResults(brs);
        return SUCCESS;
    }
    
    /**
     * 导出EXCEL
     * @return
     */
    public String findExcel(){

        ActionContext ctx = ActionContext.getContext();
        HttpServletRequest request = (HttpServletRequest) ctx
                .get(ServletActionContext.HTTP_REQUEST);

        processConfigDetails = (List<ProcessConfigDetail>)request.getSession().getAttribute("processConfigDetails");
        businessRequests=(List<BusinessRequest>)request.getSession().getAttribute("businessRequests");
        Hashtable hash=new Hashtable();
        if(processConfigDetails!=null&&processConfigDetails.size()>0){
            List<NextTep> tmps=nextTepService.find(processConfigDetails.get(0).getId());
            processConfigDetails=new ArrayList<ProcessConfigDetail>();
            for(NextTep tmp:tmps){
                if(hash.containsKey(tmp.getProcessDetail().getId())){
                    processConfigDetails.remove(hash.get(tmp.getProcessDetail().getId()));
                    hash.remove(tmp.getProcessDetail().getId());
                }
                processConfigDetails.add(tmp.getProcessDetail());
                hash.put(tmp.getProcessDetail().getId(),tmp.getProcessDetail());
            }
            processConfigDetails.add(tmps.get(tmps.size()-1).getNextConfig());
            num=processConfigDetails.size();

            if(businessRequests!=null){
                for(BusinessRequest br:businessRequests){
                    List<ProcTranRecord> rtrs=br.getProcTranRecords();
                    hash=new Hashtable();
                    for(ProcTranRecord rtr:rtrs){
                        if(rtr!=null)
                            hash.put(rtr.getProcessStep().getId(), rtr.getProcessStep().getId());
                    }
                    
                    ////获取当前步骤加入流程情况描述
                    Criterion cond3 = Restrictions.eq("refEntityId", br);
                    List<TransactProcessStatus> tpss = transactProcessStatusService.findBy(cond3);
                    TransactProcessStatus transactProcessStatus;
                    if(tpss!=null&&tpss.size()>0){
                        transactProcessStatus=tpss.get(0);
                        if(transactProcessStatus!=null&&transactProcessStatus.getCurrProcessConfigDetail()!=null)
                            hash.put(transactProcessStatus.getCurrProcessConfigDetail().getId(), transactProcessStatus.getCurrProcessConfigDetail().getId());
                        
                        //当前步骤进展信息
                        Criterion condt4 = Restrictions.eq("requestBusiness", br);
                        Criterion condts4 = Restrictions.eq("recotdId", "00000");
                        Criterion cond4 = Restrictions.and(condt4, condts4);
                        Criterion cond6 = Restrictions.eq("processConfigDetail", transactProcessStatus.getCurrProcessConfigDetail());
                        Criterion pcdcont = Restrictions.and(cond4, cond6);
                        List reqProgresses = reqProgressService.findBy(pcdcont);
                        br.setReqProgresses(reqProgresses);
                    }
                    float f=0;
                    List temp=new ArrayList();
                    for(ProcessConfigDetail pcd:processConfigDetails){
                        FieldObject fo=new FieldObject();
                        if(hash.containsKey(pcd.getId())){
                            fo.setId(1);    ////存在当前节点
                            temp.add(fo);
                            f=0;
                        }else{
                            fo.setId(0);    ////不存在当前节点
                            temp.add(fo);
                            f++;
                        }
                    }
                    f=100-f/num*100;
                    if(f>5){
                        f=f-4;
                    }
                    br.setPercent((int)f);
                    br.setDisplay(temp);
                }
            }
        }
        

        return SUCCESS;
    }
    
    public P getTransactProcessStatus()
    {
        return transactProcessStatus;
    }

    public void setTransactProcessStatus(P transactProcessStatus)
    {
        this.transactProcessStatus = transactProcessStatus;
    }

    public BusinessRequestService getBusinessRequestService()
    {
        return businessRequestService;
    }

    public void setBusinessRequestService(
            BusinessRequestService businessRequestService)
    {
        this.businessRequestService = businessRequestService;
    }

    public TransactProcessStatusService getTransactProcessStatusService()
    {
        return transactProcessStatusService;
    }

    public void setTransactProcessStatusService(
            TransactProcessStatusService transactProcessStatusService)
    {
        this.transactProcessStatusService = transactProcessStatusService;
    }

    public List<String> getFileNames()
    {
        return fileNames;
    }

    public void setFileNames(List<String> fileNames)
    {
        this.fileNames = fileNames;
    }

    public List<String> getUploadContentTypes()
    {
        return uploadContentTypes;
    }

    public void setUploadContentTypes(List<String> uploadContentTypes)
    {
        this.uploadContentTypes = uploadContentTypes;
    }

    public List<File> getUpload()
    {
        return uploads;
    }

    public void setUpload(List<File> uploads)
    {
        this.uploads = uploads;
    }

    public List<String> getUploadFileName()
    {
        return fileNames;
    }

    public void setUploadFileName(List<String> fileNames)
    {
        this.fileNames = fileNames;
    }

    public BusinessRequest getBusinessRequest()
    {
        return businessRequest;
    }

    public void setBusinessRequest(BusinessRequest businessRequest)
    {
        this.businessRequest = businessRequest;
    }

    public List<NextTep> getNextTeps()
    {
        return nextTeps;
    }

    public void setNextTeps(List<NextTep> nextTeps)
    {
        this.nextTeps = nextTeps;
    }

    public NextTepService getNextTepService()
    {
        return nextTepService;
    }

    public void setNextTepService(NextTepService nextTepService)
    {
        this.nextTepService = nextTepService;
    }

    public ProcessConfigDetailService getProcessConfigDetailService()
    {
        return processConfigDetailService;
    }

    public void setProcessConfigDetailService(
            ProcessConfigDetailService processConfigDetailService)
    {
        this.processConfigDetailService = processConfigDetailService;
    }

    public List<ProcessConfig> getProcessConfigs()
    {
        return processConfigs;
    }

    public void setProcessConfigs(List<ProcessConfig> processConfigs)
    {
        this.processConfigs = processConfigs;
    }

    public ProcessConfigService getProcessConfigService()
    {
        return processConfigService;
    }

    public void setProcessConfigService(
            ProcessConfigService processConfigService)
    {
        this.processConfigService = processConfigService;
    }

    public List<ProcessStep> getProcessSteps()
    {
        return processSteps;
    }

    public void setProcessSteps(List<ProcessStep> processSteps)
    {
        this.processSteps = processSteps;
    }

    public ProcessStepService getProcessStepService()
    {
        return processStepService;
    }

    public void setProcessStepService(ProcessStepService processStepService)
    {
        this.processStepService = processStepService;
    }

    public List<TransactorType> getTransactorTypes()
    {
        return transactorTypes;
    }

    public void setTransactorTypes(List<TransactorType> transactorTypes)
    {
        this.transactorTypes = transactorTypes;
    }

    public TransactorTypeService getTransactorTypeService()
    {
        return transactorTypeService;
    }

    public void setTransactorTypeService(
            TransactorTypeService transactorTypeService)
    {
        this.transactorTypeService = transactorTypeService;
    }

    public List<P> getTransactProcessStatusList()
    {
        return transactProcessStatusList;
    }

    public void setTransactProcessStatusList(List<P> transactProcessStatusList)
    {
        this.transactProcessStatusList = transactProcessStatusList;
    }

    public List<ProcessConfigDetail> getProcessConfigDetails()
    {
        return processConfigDetails;
    }

    public void setProcessConfigDetails(
            List<ProcessConfigDetail> processConfigDetails)
    {
        this.processConfigDetails = processConfigDetails;
    }

    public ProcTranRecord getProcTranRecord()
    {
        return procTranRecord;
    }

    public void setProcTranRecord(ProcTranRecord procTranRecord)
    {
        this.procTranRecord = procTranRecord;
    }

    public TransactorService getTransactorService()
    {
        return transactorService;
    }

    public void setTransactorService(TransactorService transactorService)
    {
        this.transactorService = transactorService;
    }

    public ProcTranRecordService getProcTranRecordService()
    {
        return procTranRecordService;
    }

    public void setProcTranRecordService(
            ProcTranRecordService procTranRecordService)
    {
        this.procTranRecordService = procTranRecordService;
    }

    public List<ProcTranRecord> getProcTranRecords()
    {
        return procTranRecords;
    }

    public void setProcTranRecords(List<ProcTranRecord> procTranRecords)
    {
        this.procTranRecords = procTranRecords;
    }

    public Transactor getTransactor()
    {
        return transactor;
    }

    public void setTransactor(Transactor transactor)
    {
        this.transactor = transactor;
    }

    public List<Transactor> getTransactors()
    {
        return transactors;
    }

    public void setTransactors(List<Transactor> transactors)
    {
        this.transactors = transactors;
    }

    public List<BusinessRequest> getBusinessRequests()
    {
        return businessRequests;
    }

    public void setBusinessRequests(List<BusinessRequest> businessRequests)
    {
        this.businessRequests = businessRequests;
    }

    public OrgRelaPersonService getOrgRelaPersonService()
    {
        return orgRelaPersonService;
    }

    public void setOrgRelaPersonService(OrgRelaPersonService orgRelaPersonService)
    {
        this.orgRelaPersonService = orgRelaPersonService;
    }

    public List<BusinessRequest> getYueBusinessRequests()
    {
        return yueBusinessRequests;
    }

    public void setYueBusinessRequests(List<BusinessRequest> yueBusinessRequests)
    {
        this.yueBusinessRequests = yueBusinessRequests;
    }
    
    public String getIsIndex()
    {
        return isIndex;
    }

    public void setIsIndex(String isIndex)
    {
        this.isIndex = isIndex;
    }

    public String getBusinessType()
    {
        return businessType;
    }

    public void setBusinessType(String businessType)
    {
        this.businessType = businessType;
    }

    public ProdAdvReq getProdAdvReq()
    {
        return prodAdvReq;
    }

    public void setProdAdvReq(ProdAdvReq prodAdvReq)
    {
        this.prodAdvReq = prodAdvReq;
    }

    public IProdAdvReqService getProdAdvReqService()
    {
        return prodAdvReqService;
    }

    public void setProdAdvReqService(IProdAdvReqService prodAdvReqService)
    {
        this.prodAdvReqService = prodAdvReqService;
    }

    public ProdReq getProdReq()
    {
        return prodReq;
    }

    public void setProdReq(ProdReq prodReq)
    {
        this.prodReq = prodReq;
    }

    public NewprodReq getNewprodReq()
    {
        return newprodReq;
    }

    public void setNewprodReq(NewprodReq newprodReq)
    {
        this.newprodReq = newprodReq;
    }

    public INewprodReqService getNewprodReqService()
    {
        return newprodReqService;
    }

    public void setNewprodReqService(INewprodReqService newprodReqService)
    {
        this.newprodReqService = newprodReqService;
    }

    public ReqDocuService getReqDocuService()
    {
        return reqDocuService;
    }

    public void setReqDocuService(ReqDocuService reqDocuService)
    {
        this.reqDocuService = reqDocuService;
    }

    public List<ReqDocu> getReqDocus()
    {
        return reqDocus;
    }

    public void setReqDocus(List<ReqDocu> reqDocus)
    {
        this.reqDocus = reqDocus;
    }

    public List<DocumentType> getDocumentTypes()
    {
        return documentTypes;
    }

    public void setDocumentTypes(List<DocumentType> documentTypes)
    {
        this.documentTypes = documentTypes;
    }

    public DocumentTypeService getDocumentTypeService()
    {
        return documentTypeService;
    }

    public void setDocumentTypeService(DocumentTypeService documentTypeService)
    {
        this.documentTypeService = documentTypeService;
    }

    public String getDocumentIds()
    {
        return documentIds;
    }

    public void setDocumentIds(String documentIds)
    {
        this.documentIds = documentIds;
    }

    public ReqProgressService<ReqProgress> getReqProgressService()
    {
        return reqProgressService;
    }

    public void setReqProgressService(
            ReqProgressService<ReqProgress> reqProgressService)
    {
        this.reqProgressService = reqProgressService;
    }

    public String getReturnMsg()
    {
        return returnMsg;
    }

    public void setReturnMsg(String returnMsg)
    {
        this.returnMsg = returnMsg;
    }

    public cn.ffcs.businessrequest.service.ReqDelayService<cn.ffcs.businessrequest.model.ReqDelay> getReqDelayService()
    {
        return reqDelayService;
    }

    public void setReqDelayService(
            cn.ffcs.businessrequest.service.ReqDelayService<cn.ffcs.businessrequest.model.ReqDelay> reqDelayService)
    {
        this.reqDelayService = reqDelayService;
    }

    public List<ReqProgress> getReqProgressess()
    {
        return reqProgressess;
    }

    public void setReqProgressess(List<ReqProgress> reqProgresses)
    {
        this.reqProgressess = reqProgresses;
    }


    public ReqDelay getReqDelay()
    {
        return reqDelay;
    }

    public void setReqDelay(ReqDelay reqDelay)
    {
        this.reqDelay = reqDelay;
    }

    public List<ReqDelay> getReqDelayss()
    {
        return reqDelayss;
    }

    public void setReqDelayss(List<ReqDelay> reqDelayss)
    {
        this.reqDelayss = reqDelayss;
    }

    public String getStartDept()
    {
        return startDept;
    }

    public void setStartDept(String startDept)
    {
        this.startDept = startDept;
    }

    public String getStartPerson()
    {
        return startPerson;
    }

    public void setStartPerson(String startPerson)
    {
        this.startPerson = startPerson;
    }

    public String getAdvice()
    {
        return advice;
    }

    public void setAdvice(String advice)
    {
        this.advice = advice;
    }

    public List<ProdAdvReq> getProdAdvReqs()
    {
        return prodAdvReqs;
    }

    public void setProdAdvReqs(List<ProdAdvReq> prodAdvReqs)
    {
        this.prodAdvReqs = prodAdvReqs;
    }

    public ReqProgress getReqProgress()
    {
        return reqProgress;
    }

    public void setReqProgress(
            ReqProgress reqProgress)
    {
        this.reqProgress = reqProgress;
    }

    public List<File> getUploadspro()
    {
        return uploadspro;
    }

    public void setUploadspro(List<File> uploadspro)
    {
        this.uploadspro = uploadspro;
    }

    public int getNum()
    {
        return num;
    }

    public void setNum(int num)
    {
        this.num = num;
    }

}
